//
//  AppDelegate.swift
//  GYXWeibo
//
//  Created by Twisal on 2016/11/8.
//  Copyright © 2016年 Twisal. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        
        window = UIWindow()
        window?.backgroundColor = UIColor.white
        window?.rootViewController = GYXMainViewController()
        window?.makeKeyAndVisible()
        window?.frame = UIScreen.main.bounds
        setupAdditions()
        loadJson()
        return true
    }



}


// MARK: - 设置额外信息
extension AppDelegate {
    fileprivate func setupAdditions() {
        // 设置hub显示时间
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        // 设置显示网络加载图标
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        // 检测设备版本号,获取授权
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound, .carPlay]) { (success, error) in
                print("授权" + (success ? "失败" : "成功"))
            }
        } else { // ios10.0以下
            
            let notifySettiongs = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(notifySettiongs)
        }

    }
}


// MARK: - 获取预制信息
extension AppDelegate {
    fileprivate func loadJson() {
        
        DispatchQueue.global().async {
            // 1, 获取json数据
            let url = Bundle.main.url(forResource: "main.json", withExtension: nil)
            let data = NSData(contentsOf: url!)
            
            // 2, 获取沙盒路径
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonPath = (docDir as NSString).appendingPathComponent("main.json")

            // 3, 写入沙盒
            data?.write(toFile: jsonPath, atomically: true)
            print("\(jsonPath)")
            
        }
        
    }
}
